<?php
//导入配置文件
	include('../../public/config.php');
	include('../../public/functions.php');
//连接数据库
	$con=@mysql_connect(HOST,USER,PASS) or die('连接数据库失败！');
//选择数据库并设置字符集
	mysql_select_db(DBNAME,$con);
	mysql_set_charset('utf8');
	//设置图片生成的路径
	$path="../../public/uploads/";
	$list=array('image/jpeg','image/png','image/gif','image/jpg');
switch ($_GET['a']){
	case 'doAdd':
	//执行图片上传
	$res=uploadFile($_FILES['picname'],$path,$list,1024000);
	//判断图片上传是否失败
	if(!$res['error']){
		//图片上传失败
		die("图片上传失败！原因:".$res['info']);
	}
	//获取上传图片的信息
	$picname=$res['info'];
	//执行图片缩放
	imageResize($picname,$path,70,70,'s_');
	imageResize($picname,$path,170,170,'m_');
	imageResize($picname,$path,213,182,'');
	//接收传递的值
	$typeid=$_POST['typeid'];
	$goods=$_POST['goods'];
	$company=$_POST['company'];
	$descr=$_POST['descr'];
	$price=$_POST['price'];
	$state=$_POST['state'];
	$store=$_POST['store'];
	$addtime=time();
	//编写sql语句
	$sql="insert into goods(typeid,goods,company,descr,price,picname,state,store,addtime) values({$typeid},'{$goods}','{$company}','{$descr}','{$price}','{$picname}',{$state},{$store},{$addtime})";
	//执行sql语句
	mysql_query($sql);
	//判断是否插入记录成功
	if(mysql_insert_id($con)>0){
		echo "<script>alert('添加成功！');window.location.href='./goodslist.php';</script>";
	}else{
		//删除添加失败后的图片
		unlink($path."s_".$picname);
		unlink($path."m_".$picname);
		unlink($path.$picname);
		echo "<script>alert('添加失败！');window.location.href='./goodsadd.php';</script>";
	}
	break;
	
	
	
	case 'doUpdate':
		//接收传递的值
		$goods=$_POST['goods'];
		$typeid=$_POST['typeid'];
		$price=$_POST['price'];
		$store=$_POST['store'];
		$state=$_POST['state'];
		$descr=$_POST['descr'];
		$addtime=time();
		$id=$_POST['id'];
		//处理图片上传
		//上传文件路径
		$path='../../public/images/';
		//查询数据库是否存在该图片
		$getsql="select picname from goods where id=".$id;
		$getres=mysql_query($getsql);
		$picname=mysql_fetch_assoc($getres);
		
		if(empty($_FILES['picname']['name'])){
			//更新数据库操作
			$usql="update goods set goods='{$goods}',typeid='{$typeid}',price='{$price}',store='{$store}',state='{$state}',descr='{$descr}',addtime={$addtime} where id={$id}";
			//var_dump($usql);die;
			//执行sql语句
			$ures=mysql_query($usql);
		//判断是否修改成功
			if($ures){
echo "<script>alert('修改成功！');window.location.href='./goodslist.php';</script>";
			}else{
echo "<script>alert('修改失败！');window.location.href='./goodslist.php';</script>";
				}
		}else{
			//var_dump($_POST['picname']);
			//用户重新上传文件
			//1.删除原有旧文件
			//echo $path.$_POST['picname'];die;
			
			if(file_exists($path.$_POST['picname']) || file_exists($path.'s_'.$_POST['picname']) || file_exists($path.'m_'.$_POST['picname'])){
				unlink($path.$_POST['picname']);
				unlink($path.'s_'.$_POST['picname']);
				unlink($path.'m_'.$_POST['picname']);
				//执行图片上传
				$res=uploadFile($_FILES['picname'],$path,$list);
					
				//判断上传文件是否失败
				if(!$res['error']){
					//图片上传失败
					die('商品图片上传失败！原因：'.$res['info']);
				}
				//获取图片名字
				$picname=$res['info'];
				//执行图片缩放
				//宽、高
				imageResize($picname,$path,70,70,'s_');
				imageResize($picname,$path,170,170,'m_');
				imageResize($picname,$path,128,213,'');
			}else{
				$res=uploadFile($_FILES['picname'],$path,$list);
					
				//判断上传文件是否失败
				if(!$res['error']){
					//图片上传失败
					die('商品图片上传失败！原因：'.$res['info']);
				}
				//获取图片名字
				$picname=$res['info'];
				//执行图片缩放
				//宽、高
				imageResize($picname,$path,70,70,'s_');
				imageResize($picname,$path,170,170,'m_');
				imageResize($picname,$path,128,213,'');
			}
			
			$usql="update goods set goods='{$goods}',typeid='{$typeid}',price='{$price}',store='{$store}',state='{$state}',descr='{$descr}',picname='{$picname}' where id={$id}";
			$ures=mysql_query($usql);
			if($ures){
				echo "<script>alert('修改成功！');window.location.href='./goodslist.php';</script>";
			}else{
				echo "<script>alert('修改失败！');window.location.href='./goodslist.php';</script>";
			}
		}
	break;
	
	
	case 'doDel':
		//接收要删除的记录的ID
		$id=$_GET['id'];
		//编写sql语句
		$sql="delete from goods where id={$id}";
		//执行sql语句
		$dres=mysql_query($sql);
		//判断是否执行成功
		if(mysql_affected_rows($con)>0){
			echo "<script>alert('删除成功！');window.location.href='goodslist.php';</script>";
		}else{
			echo "<script>alert('删除失败！');window.location.href='goodslist.php';</script>";
		}
	break;
	//关闭数据库
	mysql_close($con);
}